<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="memberships-content">
  <div class="gv-form-section" *ngIf="createMode">
    <div fxLayout="row" fxLayoutGap="10px">
      <mat-form-field appearance="outline" floatLabel="always" fxFlex="20">
        <mat-select placeholder="Type" name="memberType" [(ngModel)]="selectedMemberType">
          <mat-option [value]="'user'">
            <span>User</span>
          </mat-option>
          <mat-option [value]="'group'">
            <span>Group</span>
          </mat-option>
        </mat-select>
        <mat-hint>The member's type.</mat-hint>
      </mat-form-field>
      <mat-form-field *ngIf="selectedMemberType === 'user'" appearance="outline" floatLabel="always" fxFlex="100">
        <mat-label><mat-icon matPrefix>search</mat-icon></mat-label>
        <input matInput placeholder="Enter username" [matAutocomplete]="auto" [formControl]="userCtrl"/>
        <mat-autocomplete #auto="matAutocomplete" (optionSelected)="onUserSelectionChanged($event)" [displayWith]="displayUserFn">
          <mat-option *ngFor="let user of filteredUsers" [value]="user">
            <span>{{user.displayName}}</span> |
            <small>{{user.username}}</small> |
            <small style="color:grey;">{{user.source}}</small>
          </mat-option>
        </mat-autocomplete>
        <mat-hint>The user's name.</mat-hint>
      </mat-form-field>
      <mat-form-field *ngIf="selectedMemberType === 'group'" appearance="outline" floatLabel="always" fxFlex="100">
        <mat-select placeholder="Group" name="group" [(ngModel)]="selectedMember">
          <mat-option *ngFor="let group of filteredGroups" [value]="group.id">
            <span>{{ group.name }}</span>
          </mat-option>
        </mat-select>
        <mat-hint>The group's name.</mat-hint>
      </mat-form-field>
      <mat-form-field appearance="outline" floatLabel="always" fxFlex="20">
        <mat-select placeholder="Role" name="userRole" [(ngModel)]="selectedRole">
          <mat-option *ngFor="let role of roles" [value]="role.id" [disabled]="isRoleDisabled(role)">
            <span>{{ role.name }}</span>
          </mat-option>
        </mat-select>
        <mat-hint>The role's name.</mat-hint>
      </mat-form-field>
      <div>
        <button style="margin-top: 7px;" mat-stroked-button (click)="addMembership($event)" [disabled]="!selectedMember || !selectedRole">+ ADD</button>
      </div>
    </div>
  </div>

  <ngx-datatable class="material" *ngIf="dataLoaded"
                 [columnMode]="'flex'"
                 [headerHeight]="40"
                 [footerHeight]="40"
                 [rowHeight]="50"
                 [rows]='members'
                 [sorts]="[{ prop: 'memberType', dir: 'desc' }]">
    <ngx-datatable-column name="Name" [flexGrow]="4" [cellClass]="'membership-name-cell'">
      <ng-template let-row="row" ngx-datatable-cell-template>
        <div class="membership-header" fxLayout="row">
          <div *ngIf="row.memberType === 'user'" class="user-avatar">
            <app-user-avatar [user]="row" [width]="'40'"></app-user-avatar>
          </div>
          <div *ngIf="row.memberType === 'group'" class="membership-icon">
            <mat-icon>group</mat-icon>
          </div>
          <div class="membership-information" fxLayout="column">
            <h3>{{row.name}}</h3>
          </div>
        </div>
      </ng-template>
    </ngx-datatable-column>
    <ngx-datatable-column name="Type" [flexGrow]="1" prop="memberType">
      <ng-template let-row="row" ngx-datatable-cell-template>
        <p>{{row.memberType}}</p>
      </ng-template>
    </ngx-datatable-column>
    <ngx-datatable-column name="Role" [flexGrow]="2" prop="roleName">
      <ng-template let-row="row" ngx-datatable-cell-template>
        <mat-form-field *ngIf="isEditable(row)" style="margin-top: -12px;">
          <mat-select name="memberRole" [value]="row.roleId" (selectionChange)="update(row.memberId, row.memberType, $event)" [disabled]="!editMode">
            <mat-option *ngFor="let role of roles" [value]="role.id" [disabled]="isRoleDisabled(role)">
              <span>{{ role.name }}</span>
            </mat-option>
          </mat-select>
        </mat-form-field>
        <p class="primary-owner-membership" *ngIf="!isEditable(row)">{{row.roleName}}</p>
      </ng-template>
    </ngx-datatable-column>
    <ngx-datatable-column name="" [flexGrow]="1">
      <ng-template let-row="row" ngx-datatable-cell-template>
        <div fxLayout="row" class="members-actions" *ngIf="deleteMode && isEditable(row)">
          <button mat-icon-button (click)="delete(row.id, $event)"><mat-icon matTooltip="Delete">clear</mat-icon></button>
        </div>
      </ng-template>
    </ngx-datatable-column>
  </ngx-datatable>
</div>
